/* eslint-disable @typescript-eslint/no-var-requires */
const esbuild = require('esbuild');
const watchMod = process.argv.indexOf('--watch') >= 0;
// const sassPlugin = require('esbuild-plugin-sass');
const { vanillaExtractPlugin } = require('@vanilla-extract/esbuild-plugin');

const options = {
	entryPoints: ['src/index.tsx'],
	mainFields: ['browser', 'module', 'main'],
	bundle: true,
	minify: !watchMod,
	platform: 'browser',
	outdir: 'dist',
	sourcemap: watchMod,
	logLevel: 'info',
	jsx: 'transform',
	jsxFactory: 'h',
	jsxFragment: 'fragment',
	plugins: [
		/* sassPlugin(), */ vanillaExtractPlugin({
			esbuildOptions: {
				loader: { '.svg': 'text' },
			},
		}),
	],
	loader: {
		'.svg': 'text',
	},
};

esbuild
	.context(options)
	.then((context) => {
		if (watchMod) {
			return context.watch();
		} else {
			return context.rebuild();
		}
	})
	.catch(() => process.exit(1));
